package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import networking.SMap;

import main.Main;

@SuppressWarnings("serial")
public class Game extends JPanel implements ActionListener, KeyListener
{
	private ArrayList<Target> target = new ArrayList<Target>();
	private Snake snake;
	private HUD hud = new HUD(100);
	private boolean gameOver = true;
	private boolean start = true;
	private int mapStart = 6;
	private int map = mapStart;
	private Timer t;
	private int mapBlock = 0;
	private int resetLength = 400;
	private boolean pause = false;
	private JFrame frame;
	private Main menu;
	private boolean cMap = false;
	private boolean drawGreen = true; // remove the false when drawgreen checkbox is made. 
	private int HUDSet;
	private SMap CMAP;
	private boolean mapTest;
	private boolean campaign = true;

	public Game(JFrame f, Main m)
	{
		this(f, m, 1);
		campaign = false;
	}

	public Game(JFrame f, Main m, int i) 
	{
		this(f, m, null, i);
	}

	public Game(JFrame f, Main m, SMap testMap) 
	{
		this(f, m, testMap, 0);
	}
	
	public Game(JFrame f, Main m, SMap testMap, int i) 
	{
		mapTest = false;
		if(i == 0)
		{
			mapTest = true;
			CMAP = testMap;
			Map.setMap(testMap);
			cMap = true;
		}
		mapStart = i;
		menu = m;
		frame = f;
		addKeyListener(this);
		resetGame(0);
		setFocusable(true);
		t = new Timer(50, this);
		t.start();
		setBackground(Color.black);
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		HUDSet = 0;
		if(gameOver)
		{
			g.setColor(Color.green);
			if(start)
			{
				HUDSet = 2;
			}
			else
			{
				HUDSet = 1;
			}
		}
		else
		{
			for(int i=0; i<Map.getWidth(); i++)
			{
				for(int j=0; j<Map.getHeight(); j++)
				{
					Color location = new Color(Map.getValue(i,j));
					if(!drawGreen)
					{
						if(location.getRed() == 0 && location.getBlue() == 0 && location.getGreen() > 20)
						{
							location = Color.black;
						}
					}
					g.setColor(location);
					g.fillRect(i*10, j*10, 10, 10);
				}
			}
			snake.paint(g);
			if(!pause)
			{
				if(!t.isRunning())
				{
					t.restart();
				}

				t.setDelay(snake.speedCheck() * 25);
				
				//if(snake.getScore() != mapBlock && snake.getScore() % resetLength == 0)
				if(target.size() == 0)
				{
					if(campaign)
					{
						mapBlock += resetLength;
						map++;
						resetGame(snake.getScore());
					}
					resetTargets();
				}
				gameOver = snake.checkGameOver();
				for(int i=0; i<target.size(); i++)
				{
					target.get(i).paint(g);
				}
				
			}
			else
			{
				HUDSet = 3;
				t.stop();
			}
		}
		hud.paint(g, snake.getScore(), HUDSet, mapTest);
		//menu.paint(g);
	}

	public void resetGame(int s)
	{
		if (!cMap) 
		{
			//Map.setMap("maps/Map" + map + ".snake");
			int[][] array = new int[50][50];
			int blue = Color.blue.getRGB();

			for(int i=0; i<50; i++)
			{
				array[i][0] = blue;
				array[i][49] = blue;
				array[0][i] = blue;
				array[49][i] = blue;
			}
			SMap map = new SMap(array, "");
			Map.setMap(map);
		}
		else
		{
			
		}
		setSize(Map.getWidth()*10, Map.getHeight()*10 +hud.getHeight());
		frame.setSize(getWidth(), getHeight());
		removeKeyListener(snake);
		snake = new Snake(40, 40, s);
		snake.setTarget(target);
		resetTargets();
		addKeyListener(snake);
	}
	
	public void resetTargets()
	{
		target.clear();
		if(Map.targetCount() == 0)
		{

			for(int i=0; i<map; i++)
			{
				target.add(new Target(snake));
			}
			if(!campaign)
			{
				map++;
			}
		}
		else
		{
			for(int i=0; i<Map.targetCount(); i++)
			{
				target.add(new Target(snake, i));
			}
		}
		
	}
	public void actionPerformed(ActionEvent e)
	{
		repaint();
	}

	public void keyReleased(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_P || e.getKeyCode() == KeyEvent.VK_SPACE && !gameOver)
		{
			pause = !pause;
			if(!pause)
			{
				repaint();
			}
		}
		if(gameOver && e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			if (!cMap) 
			{
				map = mapStart;
			}
			resetGame(0);
			gameOver = false;
			mapBlock = 0;
			if(start)
			{
				start = false;
			}
		}
		if(cMap && e.getKeyCode() == KeyEvent.VK_M)
		{
			removeKeyListener(this);
			t.removeActionListener(this);
			removeKeyListener(snake);
			menu.backToEditor(CMAP);
			setFocusable(false);
		}
		else if(gameOver && e.getKeyCode() == KeyEvent.VK_M)
		{
			removeKeyListener(this);
			t.removeActionListener(this);
			removeKeyListener(snake);
			menu.backToMainMenu();
			setFocusable(false);
		}
	}

	public void keyPressed(KeyEvent e){}

	public void keyTyped(KeyEvent e){}
}
